import { defHttp } from '/@/utils/http/axios';
import { createVNode } from 'vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { Modal, message } from 'ant-design-vue';

export const API = {
  // 门闸列表分页
  page: '/entrance/page',
  // 门闸列表
  list: '/entrance/list',
  // 门闸删除
  delete: '/entrance/delete',
  // 门闸详情
  info: '/entrance/info',
  // 新增门闸信息
  add: '/entrance/insert',
  // 修改门闸信息
  modify: '/entrance/modify',
  // 更新PIC图
  updateGraph: '/entrance/update-graph',
  // 控制
  control: '/entrance/control',
  // 操作历史查询
  log: '/entrance/log/page',
  // 7日操作
  count: '/entrance/log/count',
};

/**
 * 控制
 */
export const controlApi = async (params) => {
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title: '提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: '确定执行该操作吗?',
      centered: true,
      async onOk() {
        await defHttp.post({ url: API.control, params }).then((res) => {
          message.success('操作成功');
          resolve(res);
        });
      },
      onCancel() {
        message.warning('已取消');
        resolve('');
      },
    });
  });
};

/**
 * 获取门闸分页列表
 * @param {int} current 当前页
 * @param {int} size 当前页总数
 * @returns
 */
export const entrancePageApi = async (params) => {
  return await defHttp.get({ url: API.page, params });
};

/**
 * 操作历史查询
 * @param {int} current 当前页
 * @param {int} size 当前页总数
 * @returns
 */
export const entranceLogApi = async (params) => {
  return await defHttp.get({ url: API.log, params });
};

/**
 * 7日操作
 * @returns
 */
export const entranceCountApi = async (params) => {
  return await defHttp.get({ url: API.count, params });
};

/**
 * 根据ID获取门闸详情
 * @returns
 */
export const entranceInfoApi = async (params) => {
  return await defHttp.get({ url: API.info, params });
};

/**
 * 获取门闸列表
 * @returns
 */
export const entranceListApi = async (params) => {
  return await defHttp.get({ url: API.list, params });
};

/**
 * 门闸编辑 文档地址 {@link http://192.168.1.52:8080/api/doc.html#/default/%E7%AB%99%E7%82%B9/modifyUsingPOST}
 * @param {String} name 名称
 * @param {String} address 地址
 * @param {String} contect 联系人
 * @param {String} longitue 经度
 * @param {String} latitude 纬度
 * @param {String} entranceCode 门闸编码
 * @param {String} entranceSubtype 门闸主体类型
 * @param {String} entranceType 门闸类型
 * @param {String} tel 电话号码
 * @returns
 */
export const entranceAddApi = async (data) => {
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title: '提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: '确定提交当前数据?',
      centered: true,
      async onOk() {
        await defHttp.post({ url: API.add, data }).then((res) => {
          message.success('操作成功');
          resolve(res);
        });
      },
      onCancel() {
        message.warning('已取消');
        resolve('');
      },
    });
  });
};

/**
 * 门闸编辑 文档地址 {@link http://192.168.1.52:8080/api/doc.html#/default/%E7%AB%99%E7%82%B9/modifyUsingPOST}
 * @param {String} name 名称
 * @param {String} address 地址
 * @param {String} contect
 * @param {String} longitue 经度
 * @param {String} latitude 纬度
 * @param {String} entranceCode 门闸编码
 * @param {String} entranceSubtype 门闸主体类型
 * @param {String} entranceType 门闸类型
 * @param {String} tel 电话号码
 * @returns
 */
export const entranceModifyApi = async (data) => {
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title: '提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: '确定提交当前数据?',
      centered: true,
      async onOk() {
        await defHttp.post({ url: API.modify, data }).then((res) => {
          message.success('操作成功');
          resolve(res);
        });
      },
      onCancel() {
        message.warning('已取消');
        resolve('');
      },
    });
  });
};

/**
 * 删除门闸接口
 * @param {*} id
 * @returns
 */
export const entranceDeleteApi = (data) => {
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title: '提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: '确定删除当前门闸数据?',
      centered: true,
      onOk() {
        resolve(defHttp.post({ url: API.delete, data }));
      },
      onCancel() {
        message.warning('已取消');
        reject();
      },
    });
  });
};

/**
 * 更新plc图 {@link http://192.168.1.66:38080/api/doc.html#/default/%E7%AB%99%E7%82%B9/updateGraphUsingPOST}
 * @param {String} id
 * @param {String} graph PIC图
 * @returns
 */
export const updateGraphApi = (data) => {
  return new Promise(async (resolve, reject) => {
    await defHttp
      .post({ url: API.updateGraph, data })
      .then((res) => {
        message.success('保存成功');
        resolve(res);
      })
      .catch(() => {
        message.error('保存失败');
        reject();
      });
  });
};

export default {
  pageUrl: API.page,
};
